package speed.context;

import java.util.ArrayList;
import java.util.List;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;

/**
 * @author zhaohaolin(zhaohaolin2010@gmail.com)
 */
public class SpeedApplicationContext implements SpeedContext {

	public SpeedApplicationContext() {
		//
	}

	// 单例模式,保证在此类中必须在整个应用中只有一个实例
	private static SpeedApplicationContext instance = null;

	public static SpeedApplicationContext getInstance() {
		if (null == instance) {
			synchronized (SpeedApplicationContext.class) {
				instance = new SpeedApplicationContext();
			}
		}
		return instance;
	}

	private Injector injector;

	public Injector getInjector() {
		return injector;
	}

	public void setInjector(Injector injector) {
		this.injector = injector;
	}

	public void init() {
		// 实际项目中必须采用从外部注入进来的方式
		this.injector = Guice.createInjector(modules);
	}

	public void init(List<Module> modules) {
		this.modules = modules;
		this.injector = Guice.createInjector(modules);
	}

	public List<Module> getModules() {
		return modules;
	}

	public void setModules(List<Module> modules) {
		this.modules = modules;
	}

	private List<Module> modules = new ArrayList<Module>();

}
